﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using DTO;

namespace DAO
{

	public class QuiLieuDAO:IData<QuiLieu> 
 	{
		public QuiLieuDAO(String myconnecString) : base(myconnecString) { }

		protected override int InsertOnSubmit(ref QuiLieu item, int t) {
			 SqlParameter p = new SqlParameter(); 
			 if( t == 0)
				p.Direction = ParameterDirection.Output;
			 p.ParameterName = "@" + QuiLieu.c_id;
			 p.Value = item.id;
			 Object obj = item.id; 
			int i = connec.ExecuteNonQuery("sp_QuiLieu", CommandType.StoredProcedure,p,ref obj,new SqlParameter("@flag", t),
				 new SqlParameter("@" + QuiLieu.c_linkId, item.linkId), new SqlParameter("@" + QuiLieu.c_userId, item.userId),
				 new SqlParameter("@" + QuiLieu.c_fullName, item.fullName), new SqlParameter("@" + QuiLieu.c_dateDuonglich, item.dateDuonglich),
				 new SqlParameter("@" + QuiLieu.c_amlich, item.amlich), new SqlParameter("@" + QuiLieu.c_ghichu, item.ghichu));
			if(t == 0)
				item.id = Convert.ToInt64(obj);
			return i;
		}

		public override QuiLieu GetItem(IDataReader dr)
		{
			QuiLieu obj = new QuiLieu();
			obj.id = Convert.ToInt64(dr[QuiLieu.c_id]);
			obj.linkId = Convert.ToInt32(dr[QuiLieu.c_linkId]);
			obj.userId = Convert.ToInt64(dr[QuiLieu.c_userId]);
			obj.fullName = Convert.ToString(dr[QuiLieu.c_fullName]);
			obj.dateDuonglich = Convert.ToDateTime(dr[QuiLieu.c_dateDuonglich]);
			obj.amlich = Convert.ToDateTime(dr[QuiLieu.c_amlich]);
			obj.ghichu = Convert.ToString(dr[QuiLieu.c_ghichu]);
			return obj;
		}

		public override List<QuiLieu> GetAll()
		{
			return GetAll("sp_GetAllQuiLieu", CommandType.StoredProcedure);
		}

		public override QuiLieu GetItemById(object id)
		{
			return GetAll().SingleOrDefault(p => p.id == Convert.ToInt64(id));
		}
		public override QuiLieu GetItemById(QuiLieu item){
			throw new NotImplementedException();
		}

	}

 }
